package com.jflyfox.component.controller;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.jflyfox.component.util.DateUtils;
import com.jflyfox.jfinal.component.annotation.ControllerBind;

/**
 * 2016年11月20日 14:53:49
 * 
 * uedtior的统一请求路径。serverUrl参数指定的方法
 * 参考：播种的回答。http://www.jfinal.com/feedback/541
 * 
 * @author zempty
 *
 */
@ControllerBind(controllerKey = "ueditor")
public class UeditorController extends Controller {

	public void index() {
		if ("config".equals(getPara("action"))) {
			render("/static/component/ueditor/jsp/config.json");
			return;
		}
		
		if ("uploadImage".equals(getPara("action"))) {
			UploadFile uf = getFile("upfile");
			Ret ret = Ret.create("state", "SUCCESS");
			ret.put("name", uf.getFileName());
			ret.put("original", uf.getOriginalFileName());
			ret.put("size", uf.getFile().length());
			ret.put("type", uf.getOriginalFileName().substring(uf.getOriginalFileName().lastIndexOf(".")));
			ret.put("url", JFinal.me().getConstants().getBaseUploadPath() + "/" + DateUtils.getByType("year") + "/" + DateUtils.getByType("month") + "/" + DateUtils.getByType("day") + "/" + uf.getFileName());
			
			renderJson(ret.getData());
			return;
		}
	}
	
}
